<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="院校名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入院校名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="areaId">
        <el-select
          clearable
          v-model="queryParams.areaId"
          placeholder="请选择地区"
        >
          <el-option
            v-for="item in areas"
            :key="item.name + 'w'"
            :label="item.name"
            :value="item.id"
            @keyup.enter.native="handleQuery"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="院校类型" prop="type1">
        <el-select
          clearable
          v-model="queryParams.type1"
          placeholder="请选择院校类型"
        >
          <el-option
            v-for="item in schoolTypes1"
            :key="item.name + '$'"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="办学类型" prop="type2">
        <el-select
          clearable
          v-model="queryParams.type2"
          placeholder="请选择办学类型"
        >
          <el-option
            v-for="item in schoolTypes2"
            :key="item.name + 'f'"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="修改时间" prop="modifyTime">
        <el-date-picker
          clearable
          v-model="queryParams.modifyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              >导出</el-button
            >
          </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="schoolList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="院校名称" align="center" prop="name" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="位置" align="center" prop="areaName" />
      <el-table-column label="院校类型" align="center" prop="type1Name" />
      <el-table-column label="办学类型" align="center" prop="type2Name">
      </el-table-column>
      <el-table-column label="保研率" align="center" prop="postgraduateRate">
        <template slot-scope="scope">
          <span>{{ scope.row.postgraduateRate + "%" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="国际交流项目数量"
        align="center"
        prop="internationalExchangeProjects"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
         
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        :current-page="pageNum"
        :page-size="pageSize"
        layout="total,prev, pager, next"
        :total="total"
        @prev-click="prevClick"
        @next-click="nextClick"
        @current-change="pageChange"
      >
      </el-pagination>
    </div>

    <!-- 添加或修改用户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="院校名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入院校名称" />
        </el-form-item>
        <el-form-item label="地区" prop="areaId">
          <el-select clearable v-model="form.areaId" placeholder="请选择地区">
            <el-option
              v-for="item in areas"
              :key="item.name + 'n'"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="院校类型" prop="type1">
          <el-select
            clearable
            v-model="form.type1"
            placeholder="请选择院校类型"
          >
            <el-option
              v-for="item in schoolTypes1"
              :key="item.name + 'j'"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="办学类型" prop="type2">
          <el-select
            clearable
            v-model="form.type2"
            placeholder="请选择办学类型"
          >
            <el-option
              v-for="item in schoolTypes2"
              :key="item.name + 'l'"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="保研率" prop="postgraduateRate">
          <el-input
            v-model="form.postgraduateRate"
            placeholder="请输入保研率"
          />
        </el-form-item>

        <el-form-item
          label="国际交流项目数"
          prop="internationalExchangeProjects"
        >
          <el-input
            v-model="form.internationalExchangeProjects"
            placeholder="请输入国际交流项目数"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
    
    <script>
import { getAreaList } from "@/api/constant";
import {
  getSchoolListBackend,
  addSchool,
  updateSchool,
  getSchoolById,
  delSchool,
} from "@/api/school";
export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户管理表格数据
      schoolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      pageNum: 1,
      pageSize: 10,
      queryParams: {
        name: null,
        areaId: null,
        type1: null,
        type2: null,
      },
      areas: [],
      schoolTypes1: [
        {
          id: "1",
          name: "综合",
        },
        {
          id: "2",
          name: "理工",
        },
        {
          id: "3",
          name: "农林",
        },
        {
          id: "4",
          name: "医药",
        },
        {
          id: "5",
          name: "师范",
        },
        {
          id: "6",
          name: "语言",
        },
        {
          id: "7",
          name: "财经",
        },
        {
          id: "8",
          name: "体育",
        },
        {
          id: "9",
          name: "艺术",
        },
      ],
      schoolTypes2: [
        {
          id: "1",
          name: "本科",
        },
        {
          id: "2",
          name: "专职",
        },
        {
          id: "3",
          name: "公办",
        },
        {
          id: "4",
          name: "民办",
        },
        {
          id: "5",
          name: "中外合作办学",
        },
        {
          id: "6",
          name: "内地与港澳台地区合作办学",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // openid: [
        //   { required: true, message: "用户id不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    getAreaList().then((res) => {
      this.areas = res.data.areas;
    });
    this.getList();
  },
  methods: {
    nextClick(page) {
      page++;
      if (page > Math.ceil(this.total / this.pageSize)) {
        this.pageNum = Math.ceil(this.total / this.pageSize);
      } else {
        this.pageNum = page;
      }
    },
    prevClick(page) {
      page--;
      if (page <= 0) {
        this.pageNum = 1;
      } else {
        this.pageNum = page;
      }
    },
    pageChange(page) {
      this.pageNum = page;
      this.getList();
    },
    /** 查询用户管理列表 */
    getList() {
      this.loading = true;
      getSchoolListBackend(this.queryParams, this.pageNum, this.pageSize).then(
        (response) => {
          this.schoolList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        areaId: null,
        type1: null,
        type2: null,
        type1Name: null,
        type2Name: null,
        postgraduateRate: null,
        internationalExchangeProjects: null,
        phone: null,
        gender: null,
        createTime: null,
        modifyTime: null,
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        name: null,
        areaId: null,
        type1: null,
        type2: null,
      }),
        this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加院校管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSchoolById(id).then((response) => {
        this.form = response.data.school;
        this.open = true;
        this.title = "修改院校管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.areas.forEach((item) => {
              if (this.form.areaId == item.id) {
                this.form.areaName = item.name;
              }
            });
            this.schoolTypes1.forEach((item) => {
              if (this.form.type1 == item.id) {
                this.form.type1Name = item.name;
              }
            });
            this.schoolTypes2.forEach((item) => {
              if (this.form.type2 == item.id) {
                this.form.type2Name = item.name;
              }
            });
            updateSchool(this.form).then((response) => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.open = false;
              this.getList();
            });
          } else {
            this.areas.forEach((item) => {
              console.log(item);
              if (this.form.areaId == item.id) {
                this.form.areaName = item.name;
              }
            });
            this.schoolTypes1.forEach((item) => {
              if (this.form.type1 == item.id) {
                this.form.type1Name = item.name;
              }
            });
            this.schoolTypes2.forEach((item) => {
              if (this.form.type2 == item.id) {
                this.form.type2Name = item.name;
              }
            });
            addSchool(this.form).then((response) => {
              this.$message({
                message: "新增成功",
                type: "success",
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除院校数据项？")
        .then(function () {
          return delSchool(ids);
        })
        .then(() => {
          this.getList();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        })
        .catch(() => {
          this.$message({
            message: "取消删除",
          });
        });
    },
  },
};
</script>
  <style scoped lang="scss">
.page {
}
</style>
    